package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.TravelMapper;
import cn.wolfcode.trip.base.mapper.UserMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IUserService;
import cn.wolfcode.trip.base.util.UserContext;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private TravelMapper travelMapper;
    @Override
    public User getUser(Long id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean selectCollectByUserId(Long userId, Long travelId) {
        Map travel = travelMapper.selectCollectByUserId(userId,travelId);
        if(travel != null){
            return true;
        }
        return false;
    }

    @Override
    public void saveCollectByUserId(Long userId, Long travelId) {
        travelMapper.saveCollect(userId,travelId);
    }

    @Override
    public void deleteCollectByUserId(Long userId, Long travelId) {
        travelMapper.deleteCollect(userId,travelId);
    }


    @Override
    public PageInfo<User> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<User> list = userMapper.selectForList(qo);
        return new PageInfo<>(list);
    }

    @Override
    public User login(User user) {

        if (StringUtils.hasLength(user.getEmail()) && StringUtils.hasLength(user.getPassword())) {
            User temp = userMapper.selectByEmailAndPassword(user.getEmail(), user.getPassword());
            if (temp == null) {
                throw new RuntimeException("亲,邮箱和密码不匹配");
            }else{
                //登陆成功  将用户存到session中 共后期校验
                UserContext.setCurrentUser(temp);
                return temp;
            }
        }

        throw new RuntimeException("亲,请输入邮箱和密码");

    }

    @Override
    public void saveOrUpdate(User user) {
        if (user.getId() != null) {
            userMapper.updateByPrimaryKey(user);
        }else{
            //保存之前 查询数据库 判断邮箱是否已经注册
            User temp =userMapper.selectByEmailAndPassword(user.getEmail(),null);

            if (temp != null) {
                //如果被注册就抛出一个异常
                throw new RuntimeException("亲,邮箱已经被注册");
            }else{
                //如果没有没注册 就保存 并设置一些默认值
                user.setGender(User.GENDER_SECRET);
                user.setCoverImgUrl("/img/user/bg.jpeg");
                user.setHeadImgUrl("/img/user/head.jpg");
                userMapper.insert(user);
            }

        }
    }

    @Override
    public int selectCountTravelById(Long userId) {
       int  travelCount = userMapper.selectCountTravelById(userId);
        return travelCount;
    }

    @Override
    public int selectTravelCommendCountTravelById(Long userId) {
        int  travelCommendCount = userMapper.selectTravelCommendCountTravelById(userId);
        return travelCommendCount;
    }

    @Override
    public int selectStrategyCommendCountStrategyById(Long userId) {
        int  strategyCommendCount = userMapper.selectStrategyCommendCountStrategyById(userId);
        return strategyCommendCount;
    }

}
